﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Security.Permissions;
using System.Collections.Specialized;

namespace GandA.Web.UI.Controls
{
    [
    AspNetHostingPermission(SecurityAction.Demand,
        Level = AspNetHostingPermissionLevel.Minimal),
    AspNetHostingPermission(SecurityAction.InheritanceDemand,
        Level = AspNetHostingPermissionLevel.Minimal),
    ]
    [DefaultProperty("Checked")]
    [ToolboxData("<{0}:CheckBox runat=server></{0}:CheckBox>")]
    [ToolboxBitmap(typeof(System.Web.UI.WebControls.CheckBox))]
    public class CheckBox : WebControl, IPostBackEventHandler
    {
        public EventHandler _check_changed;

        #region propiedades
        [Bindable(true)]
        [Category("Default")]
        [DefaultValue(false)]
        [Localizable(true)]
        public bool Checked
        {
            get
            {
                return ViewState["_Checked"] == null ? false : (bool)ViewState["_Checked"];
            }
            set
            {
                ViewState["_Checked"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                return ViewState["_Text"] == null ? string.Empty : ViewState["_Text"].ToString();
            }

            set
            {
                ViewState["_Text"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Value
        {
            get
            {
                return ViewState["_Value"] == null ? string.Empty : ViewState["_Value"].ToString();
            }

            set
            {
                ViewState["_Value"] = value;
            }
        }

        [Bindable(true)]
        [Category("Behavior")]
        [DefaultValue(false)]
        [Localizable(true)]
        public bool AutoPostBack
        {
            get
            {
                return ViewState["_AutoPostBack"] == null ? false : (bool)ViewState["_AutoPostBack"];
            }

            set
            {
                ViewState["_AutoPostBack"] = value;
            }
        }

        [Bindable(true)]
        [Category("Layout")]
        [DefaultValue("")]
        [Localizable(true)]
        public override Unit Width
        {
            get
            {
                return ViewState["_Width"] == null ? (Unit)50 : (Unit)ViewState["_Width"];
            }

            set
            {
                ViewState["_Width"] = value;
            }
        }
        #endregion

        #region Eventos
        public event EventHandler OnCheckedChanged
        {
            add { _check_changed += value; }
            remove { _check_changed -= value; }
        }

        protected virtual void FireCheckChangedEvent()
        {
            if (_check_changed != null)
            {
                _check_changed(this, new EventArgs());
                this.Checked = !this.Checked;
            }
        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            //writer.AddAttribute(HtmlTextWriterAttribute.Onchange, 
            //Page.ClientScript.GetPostBackEventReference(this, String.Empty));
            base.AddAttributesToRender(writer);
        }

        public void RaisePostBackEvent(string eventArgument)
        {
            FireCheckChangedEvent();
        }
        #endregion

        protected override void OnPreRender(EventArgs e)
        {
            bool linkIncluded = false;
            foreach (Control c in Page.Header.Controls)
            {
                if (c.ID == "StyleControls")
                {
                    linkIncluded = true;
                }
            }
            if (!linkIncluded)
            {
                HtmlGenericControl csslink = new HtmlGenericControl("link");
                csslink.ID = "StyleControls";
                csslink.Attributes.Add("href",
                                       Page.ClientScript.GetWebResourceUrl(this.GetType(), "GandA.Web.UI.Controls.Controls.css"));
                csslink.Attributes.Add("type", "text/css");
                csslink.Attributes.Add("rel", "stylesheet");
                Page.Header.Controls.Add(csslink);
            }

            base.OnPreRender(e);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            PostBackOptions p = new PostBackOptions(this);
            if (this.AutoPostBack)
                writer.AddAttribute(HtmlTextWriterAttribute.Onchange, Page.ClientScript.GetPostBackEventReference(p));
            writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
            writer.AddAttribute(HtmlTextWriterAttribute.Name, this.ClientID);
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "width:" + (Width.Value + 25).ToString() + (Width.Type == UnitType.Pixel ? "px" : "%"));
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            //writer.Write("<div stlyle='width:16px'  onchange='__doPostBack(\"" + this.UniqueID + "\",\"\")' oncheckedchanged='" + this.ID + "_OnCheckedChanged'>");
            writer.WriteFullBeginTag("input type='checkbox' name='" + this.ID + "_chk' id='" + this.ID + "_chk' value='" + (string.IsNullOrEmpty(this.Value.Trim()) ? "SinValor" : this.Value.Trim()) + "' " + (this.Checked ? "checked='checked'" : "") + " class='check_02' /");
            writer.Write("<label for='" + this.ID + "_chk'>" + "Opción" + "</label>");
            writer.Write("<label for='" + this.ID + "_chk' style='width:" + Width.ToString() + "' class='check_02'>" + this.Text + "</label>");
            writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID + "_other");
            writer.AddAttribute(HtmlTextWriterAttribute.Name, this.ClientID + "_other");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Display, "none");
            base.Render(writer);
            writer.RenderEndTag();
        }

    }
}
